package com.seadragon.app.ksr.json.gson;

import static com.seadragon.app.ksr.model.KsrConstants.gf;

import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

import com.google.gson.TypeAdapter;
import com.google.gson.stream.JsonReader;
import com.google.gson.stream.JsonToken;
import com.google.gson.stream.JsonWriter;
import com.vividsolutions.jts.geom.Coordinate;
import com.vividsolutions.jts.geom.LinearRing;
import com.vividsolutions.jts.geom.MultiPolygon;
import com.vividsolutions.jts.geom.Polygon;

public class MultiPolygonAdapter extends TypeAdapter<MultiPolygon> {
	@Override
	public MultiPolygon read(JsonReader reader) throws IOException {
//		if (reader.peek() == JsonToken.NULL) {
//			reader.nextNull();
//			return null;
//		}
		double lat = 0d;
		double lon = 0d;
		String type;
		List<Polygon> polygons = new ArrayList<Polygon>();
		Coordinate coor = null;
		Polygon polygon = null;
        JsonToken token = reader.peek();
        List<Coordinate> coordinates = new ArrayList<Coordinate>();
        if (token.equals(JsonToken.BEGIN_OBJECT)) {
            reader.beginObject();
            while (!reader.peek().equals(JsonToken.END_OBJECT)) {
                if (reader.peek().equals(JsonToken.NAME)) {
                	String name = reader.nextName();
                    if (name.equals("type")){
                    	type = reader.nextString();
                    }else if(name.equals("coordinates")){
                    	token = reader.peek();
                    	if(token.equals(JsonToken.BEGIN_ARRAY)){
                        	reader.beginArray();
                        	while(reader.hasNext()){
                        		token = reader.peek();
                        		if(token.equals(JsonToken.BEGIN_ARRAY)){
                        			LinearRing shell = null;
                        			List<LinearRing> holes = new ArrayList<LinearRing>();
                                	reader.beginArray();
                                	while(reader.hasNext()){
                                		token = reader.peek();
                                		if(token.equals(JsonToken.BEGIN_ARRAY)){
                                        	reader.beginArray();
                                    		token = reader.peek();
                                        	while(!reader.peek().equals(JsonToken.END_ARRAY)){
                                        		if(token.equals(JsonToken.BEGIN_ARRAY)){
                                                	reader.beginArray();
                                        			lon = reader.nextDouble();
                                        			lat = reader.nextDouble();
                                        			coor = new Coordinate(lon, lat);
                                        			coordinates.add(coor);
                                        			reader.endArray();
                                        		}
                                        	}
                                		}
                                		reader.endArray();
                                		if(shell == null){
                                			shell = gf.createLinearRing(coordinates.toArray(new Coordinate[coordinates.size()]));
                                		}else{
                                			holes.add(gf.createLinearRing(coordinates.toArray(new Coordinate[coordinates.size()])));
                                		}
                                		coordinates = new ArrayList<Coordinate>();
                                	}
                                	reader.endArray();
                                	polygons.add(gf.createPolygon(shell, holes.toArray(new LinearRing[holes.size()])));
                        		}
                        	}
                    		token = reader.peek();
                    		if(token.equals(JsonToken.END_ARRAY)){
                    			reader.endArray();
                    		}
                    	}
                    }else{
                        reader.skipValue();
                    }
                }
            }
            reader.endObject();
        }
		return gf.createMultiPolygon(polygons.toArray(new Polygon[polygons.size()]));
	}

	@Override
	public void write(JsonWriter out, MultiPolygon value) throws IOException {
		// TODO Auto-generated method stub
		
	}

}